//
//  WMCustomPageController.m
//  CEX
//
//  Created by xiebin on 2018/1/9.
//  Copyright © 2018年 mingthink. All rights reserved.
//

#import "TrendPageController.h"
#import "SocketRocketUtility.h"
@interface TrendPageController ()<SRWebSocketDelegate>
{
    NSMutableArray *marr_local;
    NSMutableArray *marr_huobi;
    NSMutableArray *marr_bitfinex;
    NSMutableArray *marr_binance;
}
@end

@implementation TrendPageController
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
      //[self requestGetCoinTrendData];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    marr_local =[[NSMutableArray alloc]init];
    marr_huobi =[[NSMutableArray alloc]init];
    marr_bitfinex=[[NSMutableArray alloc]init];
    marr_binance =[[NSMutableArray alloc]init];
    [self initNavigationView];
    [self addMenuViewBottomLine];
    
}
- (void)initNavigationView{
    self.navView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, DEVW, NAVBAR_HEIGH)];
    self.navView.backgroundColor = GET_HEX_COLOR(NAVBAR_BG_COLOR);
    [self.view addSubview:self.navView];
    //self.view_line = view_line;
}
-(void)addMenuViewBottomLine{
    UIView *view_separateLine = UIView.new;
    view_separateLine.tag = 110;
    [self.menuView addSubview:view_separateLine];
    view_separateLine.backgroundColor = GET_HEX_COLOR(TABLE_SEPARATOR_GRAY);
    view_separateLine.layer.shadowColor = GET_HEX_COLOR(TABLE_SEPARATOR_GRAY).CGColor;//shadowColor阴影颜色
    view_separateLine.layer.shadowOffset = CGSizeMake(0,1);//shadowOffset阴影偏移,x向右偏移0，y向下偏移4
    view_separateLine.layer.shadowOpacity = 0.9;//阴影透明度，默认0
    view_separateLine.layer.shadowRadius = 1;
    view_separateLine.sd_layout.bottomSpaceToView(self.menuView,0).leftEqualToView(self.menuView).rightEqualToView(self.menuView).heightIs(0.5);
}
- (CGRect)pageController:(WMPageController *)pageController preferredFrameForMenuView:(WMMenuView *)menuView {
    return CGRectMake(0, NAVBAR_HEIGH, DEVW, 40);
}
- (CGRect)pageController:(WMPageController *)pageController preferredFrameForContentView:(WMScrollView *)contentView {
        CGFloat originY = CGRectGetMaxY([self pageController:pageController preferredFrameForMenuView:self.menuView]);
    return CGRectMake(0, originY, DEVW, self.view.frame.size.height -originY-TABBAR_HEIGH);
}
#pragma mark-------- 网络请求
-(void)requestGetCoinTrendData{
    NSMutableDictionary *paramsDict = [[NSMutableDictionary alloc]init];
//    paramsDict[@"map"] =@"home_goods";
//    paramsDict[@"type"] =type;
//    paramsDict[@"count"] =@"10";
    [HttpManager requestWithProgressTitle:@"" method:HTTPS_POST path:URL_GET_LOCAL_COIN_TREND paramDict:paramsDict finishBlock:^(id result, AFHTTPRequestOperationManager *manager) {
        
//        SocketRocketUtility *socketUtility1=[SocketRocketUtility instance];
//        [socketUtility1 SRWebSocketOpenWithURLString:@"wss://api.huobi.pro/ws"];
//        socketUtility1 sendData:<#(id)#>
        _arr_data=result;
        [[NSNotificationCenter defaultCenter] postNotificationName:HOME_DATA_REFRESH_NOTIFICATION object:nil];
//        [mainTabV.mj_header endRefreshing];
//        [mainTabV.mj_footer endRefreshing];
    }];
}
-(void)requestGetCoinList{
    NSMutableDictionary *paramsDict = [[NSMutableDictionary alloc]init];
    //    paramsDict[@"map"] =@"home_goods";
    //    paramsDict[@"type"] =type;
    //    paramsDict[@"count"] =@"10";
    [HttpManager requestWithProgressTitle:@"" method:HTTPS_POST path:URL_GET_COINLIST paramDict:paramsDict finishBlock:^(id result, AFHTTPRequestOperationManager *manager) {
        NSDictionary *dict=result;
        for (NSString *key in dict) {
            NSDictionary *dict2=dict[key];
            NSInteger type=[dict2[@"type"] integerValue];
            //            NSMutableArray *arr_local =[[NSMutableArray alloc]init];
            //            NSMutableArray *arr_huobi =[[NSMutableArray alloc]init];
            //            NSMutableArray *arr_bitfinex=[[NSMutableArray alloc]init];
            //            NSMutableArray *arr_binance =[[NSMutableArray alloc]init];
            if (type==1) {//本地交易对
                [marr_local addObject:dict2];
            }else if(type==2){//火币交易对
                [marr_huobi addObject:dict2];
            }else if(type==3){//B网交易对
                [marr_bitfinex addObject:dict2];
            }else if(type==4){//币安交易对
                [marr_binance addObject:dict2];
            }
        }
        NSURLRequest *request=[NSURLRequest requestWithURL:[NSURL URLWithString:@"wss://api.huobipro.com/ws"]];
        SRWebSocket * tempWebSocket = [[SRWebSocket alloc] initWithURLRequest:request];
        tempWebSocket.delegate = self;
        [tempWebSocket open];
        
        //[[NSNotificationCenter defaultCenter] postNotificationName:HOME_DATA_REFRESH_NOTIFICATION object:nil];
        //        [mainTabV.mj_header endRefreshing];
        //        [mainTabV.mj_footer endRefreshing];
        
        
    }];
}
- (void)webSocketDidOpen:(SRWebSocket *)webSocket{
    for (NSDictionary *dict in marr_huobi) {
        NSString *coin1=[dict[@"bitfinex_mark"] lowercaseString];
        NSString *coin2=[dict[@"trade_mark"] lowercaseString];
        NSString *tradePair=[NSString stringWithFormat:@"%@%@",coin1,coin2];
        NSDictionary *dict=@{@"sub":[NSString stringWithFormat:@"market.%@.detail",tradePair],@"id":@"id10"};
        [webSocket send:[dict mj_JSONString]];
    }
}
- (void)webSocket:(SRWebSocket *)webSocket didReceiveMessage:(id)message{
    NSData *data=[MyUtility uncompressZippedData:message];
    NSDictionary *dict=[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
    if (dict[@"ping"]) {
        NSDictionary *dict1=@{@"pong":dict[@"ping"]};
        [webSocket send:[dict1 mj_JSONString]];
    }
    NSLog(@"-------json:%@",dict);
}
- (void)webSocket:(SRWebSocket *)webSocket didFailWithError:(NSError *)error{
    
}
- (void)webSocket:(SRWebSocket *)webSocket didCloseWithCode:(NSInteger)code reason:(NSString *)reason wasClean:(BOOL)wasClean{
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
